package org.jeecg.modules.performance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.performance.Model.PerformanceConfigModel;
import org.jeecg.modules.performance.Model.UserPerforModel;
import org.jeecg.modules.performance.entity.PerformanceConfig;
import org.jeecg.modules.performance.entity.UserPerformance;
import org.jeecg.modules.performance.entity.UserRefPerfor;
import org.jeecg.modules.performance.mapper.PerformanceConfigMapper;
import org.jeecg.modules.performance.mapper.UserPerformanceMapper;
import org.jeecg.modules.performance.mapper.UserRefPerforMapper;
import org.jeecg.modules.performance.service.IPerformanceConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 绩效
 * @Author: jeecg-boot
 * @Date: 2021-04-14
 * @Version: V1.0
 */
@Service
public class PerformanceConfigServiceImpl extends ServiceImpl<PerformanceConfigMapper, PerformanceConfig> implements IPerformanceConfigService {
    @Autowired
    private PerformanceConfigMapper performanceConfigMapper;
    @Autowired
    private UserPerformanceMapper userPerformanceMapper;
    @Autowired
    private UserRefPerforMapper userRefPerforMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editPerformances(List<PerformanceConfig> performanceConfigList) {
        QueryWrapper<UserRefPerfor> queryWrapper = new   QueryWrapper<UserRefPerfor>();
        List<String> perforIds = new ArrayList<>();
        for (PerformanceConfig performanceConfig : performanceConfigList) {

            perforIds.add(performanceConfig.getId());
        }
        List<UserRefPerfor> userRefPerforList = userRefPerforMapper.selectList(null);
        if(!CollectionUtils.isEmpty(userRefPerforList)){
            Map<String, PerformanceConfig> collect = performanceConfigList.stream().collect(Collectors.toMap(PerformanceConfig::getId, x -> x));

            for (UserRefPerfor userRefPerfor : userRefPerforList) {
                if(collect.get(userRefPerfor.getPerforId())==null){
                    throw new JeecgBootException("不能删除已经引用的岗位绩效");

                }
            }
        }
        this.remove(null);
        this.saveBatch(performanceConfigList);
    }


    @Override
    public Page<UserPerforModel> getUserPerformanceList(Page<UserPerforModel> page, UserPerforModel userPerforModel) {
        List<UserPerforModel> userPerformanceList = performanceConfigMapper.getUserPerformanceList(page, userPerforModel);
        QueryWrapper<PerformanceConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", "post");
        //查询岗位业绩
        List<PerformanceConfig> performanceConfigList = performanceConfigMapper.selectList(queryWrapper);
        for (UserPerforModel perforModel : userPerformanceList) {
            //岗位绩效
            Float totalScore = 0f;
            //加权绩效
            Float performanceValue = 0f;
            //自己配置的绩效
            List<PerformanceConfigModel> performanceConfigModelList = perforModel.getPerformanceConfigModelList();
            List<PerformanceConfigModel> resultPerList = new ArrayList<>();
            // 计算是否有岗位配置 并且相应岗位绩效设置的值
            if (!CollectionUtils.isEmpty(performanceConfigList)) {
                //所有的岗位绩效
                for (PerformanceConfig performanceConfig : performanceConfigList) {
                    PerformanceConfigModel performanceConfigModel = new PerformanceConfigModel();
                    performanceConfigModel.setName(performanceConfig.getName());
                    performanceConfigModel.setPerformanceId(performanceConfig.getId());
                    performanceConfigModel.setScore(performanceConfig.getScore());
                    if (!CollectionUtils.isEmpty(performanceConfigModelList)) {
                             //如果自己配置岗位绩效
                        Map<String, PerformanceConfigModel> collectMap = performanceConfigModelList.stream().collect(Collectors.toMap(PerformanceConfigModel::getPerformanceId, p -> p));

                        if (!CollectionUtils.isEmpty(collectMap)) {
                            PerformanceConfigModel performanceConfigModelColl = collectMap.get(performanceConfig.getId());
                            if (performanceConfigModelColl == null) {
                                performanceConfigModel.setIsSelect(false);


                            } else {
                                String postPerforms = perforModel.getPostPerforms();
                                if (StringUtils.isEmpty(postPerforms)) {
                                    perforModel.setPostPerforms(performanceConfigModelColl.getName());

                                } else {
                                    perforModel.setPostPerforms(postPerforms + "," + performanceConfigModelColl.getName());

                                }
                                performanceConfigModel.setIsSelect(true);
                                totalScore += Float.valueOf(performanceConfig.getScore());

                            }


                        }
                    } else {
                        performanceConfigModel.setIsSelect(false);
                    }
                    resultPerList.add(performanceConfigModel);

                }

                perforModel.setPostValue(totalScore.toString());
                perforModel.setPerformanceConfigModelList(resultPerList);


                if (perforModel.getFixType() != null && perforModel.getFixType().equals(1)) {
                    performanceValue += perforModel.getFixSalary().floatValue();
                }
                if (!CollectionUtils.isEmpty(performanceConfigModelList)) {
                    for (PerformanceConfigModel performanceConfigModel : performanceConfigModelList) {
                        performanceValue += Float.valueOf(performanceConfigModel.getScore());
                    }

                }

                if (perforModel.getFixType() != null && perforModel.getFixType().equals(1)) {
                    perforModel.setSalaryName("固定薪资");
                } else {
                    if (!CollectionUtils.isEmpty(performanceConfigModelList)) {
                        perforModel.setSalaryName("绩效薪资");

                    } else {
                        perforModel.setSalaryName("未分配");

                    }
                }

                perforModel.setPerformanceValue(performanceValue.toString());

            }

        }
        return page.setRecords(userPerformanceList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setUserPerformances(UserPerforModel userPerforModel) {
        List<PerformanceConfigModel> performanceConfigModelList = userPerforModel.getPerformanceConfigModelList().stream().filter(u -> u.getIsSelect()).collect(Collectors.toList());
//        if(CollectionUtils.isEmpty(performanceConfigModelList)){
//            throw  new JeecgBootException("请选择岗位绩效");
//        }

        QueryWrapper<UserPerformance> userWrapper = new QueryWrapper<>();
        userWrapper.eq("user_id",userPerforModel.getUserId());

        UserPerformance userPerformance = userPerformanceMapper.selectOne(userWrapper);
        if(userPerformance !=null){
            QueryWrapper<UserRefPerfor> userRefPerWrapper = new QueryWrapper<>();
            userRefPerWrapper.eq("user_perfor_id",userPerformance.getId());
            userRefPerforMapper.delete(userRefPerWrapper);
        }else {
            userPerformance = new  UserPerformance();
        }
        userPerformanceMapper.delete(userWrapper);

        userPerformance.setPerformanceType(Integer.valueOf(userPerforModel.getPerformanceType()));
        userPerformance.setRemark(userPerforModel.getRemark());
        userPerformance.setUserId(userPerforModel.getUserId());
        userPerformance.setFixType(userPerforModel.getFixType());
        userPerformance.setFixSalary(userPerforModel.getFixSalary());
        userPerformance.setValue(userPerforModel.getValue());
        userPerformanceMapper.insert(userPerformance);
       // userPerforModel.get
        if(!CollectionUtils.isEmpty(performanceConfigModelList)){
        for (PerformanceConfigModel performanceConfigModel : performanceConfigModelList) {
            UserRefPerfor userRefPerfor = new UserRefPerfor();
            userRefPerfor.setPerforId(performanceConfigModel.getPerformanceId());
            userRefPerfor.setUserPerforId(userPerformance.getId());
            userRefPerforMapper.insert(userRefPerfor);

        }
        }
    }
}
